//
//  SearchBar.swift
//  test1
//
//  Created by Apple on 2021/6/8.
//

/*
 参数：
 callBack(可选):使用的回调函数
 */

import SwiftUI

struct SearchBar: View {
    @EnvironmentObject var modelData: HistoryListModelData
    var callBack : ViewCallBack?
    var width: CGFloat = UIScreen.main.bounds.width
    var body: some View {
        HStack{
            TextField("请输入搜索内容", text: $modelData.Searchtext, onCommit: { modelData.isEditing = false;callBack?.goToView(to: .IndexView, from: .SearchView, intent: .Default, with: [.wd:modelData.Searchtext]);modelData.Searchtext = "";})
                .autocapitalization(.none)
                .disableAutocorrection(true)
                .frame(minWidth: 0.5*width, maxWidth: width, minHeight: 30, idealHeight: 30, maxHeight: 30)
                .padding(.horizontal, 25)
                .background(RoundedRectangle(cornerRadius: 15).foregroundColor(Color(.systemGray5)))
                .mask(RoundedRectangle(cornerRadius: 15))
                .overlay(
                    HStack{
                        Image(systemName: "magnifyingglass")
                            .foregroundColor(.gray)
                            //                            .frame(minWidth: 0,  maxWidth: /*@START_MENU_TOKEN@*/.infinity/*@END_MENU_TOKEN@*/,  alignment: .leading)
                            .background(Color(.systemGray5))
                            .padding(.leading, 5)
                            .transition(.opacity)
                            .animation(.easeInOut)
                        Spacer()
                        if modelData.isEditing {
                            Button(action: {
                                modelData.Searchtext = ""
                            }) {
                                Image(systemName: "multiply.circle.fill")
                                    .foregroundColor(.gray)
                                    .background(Color(.systemGray5))
                                    .padding(.trailing, 5)
                                    .transition(.opacity)
                                    .animation(.easeInOut)
                            }
                            
                        }
                    }
                )
                .padding(.horizontal, 10)
                .autocapitalization(.none)
                .disableAutocorrection(true)
                .onTapGesture {
                    modelData.isEditing = true
                }
                
            
            //            CustomTextField(text: $modelData.Searchtext,  defaultText: "请输入搜索内容", keyboardType: .default,width:width*0.6)
            //                .border(/*@START_MENU_TOKEN@*/Color.black/*@END_MENU_TOKEN@*/, width: /*@START_MENU_TOKEN@*/1/*@END_MENU_TOKEN@*/)
            //                .overlay(
            //                    HStack{
            //                        Image(systemName: "magnifyingglass")
            //                            .foregroundColor(.gray)
            //                            //                            .frame(minWidth: 0,  maxWidth: /*@START_MENU_TOKEN@*/.infinity/*@END_MENU_TOKEN@*/,  alignment: .leading)
            //                            .background(Color(.systemGray5))
            //                            .padding(.leading, 5)
            //                            .transition(.opacity)
            //                            .animation(.easeInOut)
            //                        Spacer()
            //                        if modelData.isEditing {
            //                            Button(action: {
            //                                modelData.Searchtext = ""
            //                            }) {
            //                                Image(systemName: "multiply.circle.fill")
            //                                    .foregroundColor(.gray)
            //                                    .background(Color(.systemGray5))
            //                                    .padding(.trailing, 5)
            //                                    .transition(.opacity)
            //                                    .animation(.easeInOut)
            //                            }
            //
            //                        }
            //                    }
            //                    )
            //                .frame(maxWidth:0.6*width).fixedSize()
            //
            //                    .lineLimit(2)
            //                    .onTapGesture {
            //                        modelData.isEditing = true
            //                    }
            //                    .zIndex(-1)
            //
            //
            //
            //
            //                    .frame(minWidth: 0.5*width, maxWidth: width, minHeight: 30, idealHeight: 30, maxHeight: 30)
            //                    .background(RoundedRectangle(cornerRadius: 15).foregroundColor(Color(.systemGray5)))
            //                .frame(width: 400.0)
            //                .mask(RoundedRectangle(cornerRadius: 15))
            //                    .padding(.leading, 5)
            
            
            if modelData.isEditing {
                Button(action:{
                    modelData.isEditing = false
                    modelData.Searchtext = ""
                    callBack?.goBack()
                    
                }){
                    Text("取消")
                }
                .padding(.trailing, 10)
                .transition(.move(edge: .trailing))
                .animation(.default)
            }
        }
    }
}

struct SearchBar_Previews: PreviewProvider {
    static var previews: some View {
        SearchBar()
            .environmentObject(HistoryListModelData())
    }
}

